import 'package:flutter/material.dart';
import 'package:flutter_company_app/model/news.dart';
import 'package:flutter_company_app/services/news.dart';

class NewsPage extends StatefulWidget {
  const NewsPage({Key key}) : super(key: key);

  @override
  _NewsPageState createState() => _NewsPageState();
}

class _NewsPageState extends State<NewsPage> {
  NewsListModel listData = NewsListModel.fromJson([]);

  void getProductList() async {
    var data = await getNewsResult();
    NewsListModel list = NewsListModel.fromJson(data);
    setState(() {
      // 一次性将所获取的内容全部填充listData中
      listData.data.addAll(list.data);
    });
  }

  @override
  void initState() {
    super.initState();
    getProductList();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: ListView.separated(
            itemBuilder: (BuildContext context, int index) => ListTile(
                  title: Text(listData.data[index].title),
                  subtitle: Text(listData.data[index].content,
                      maxLines: 2, overflow: TextOverflow.ellipsis),
                  leading: Icon(Icons.fiber_new),
                  trailing: Icon(Icons.arrow_forward),
                  contentPadding: EdgeInsets.all(10),
                  enabled: true,
                  onTap: () {
                    // 跳转
                  },
                ),
            separatorBuilder: (BuildContext context, int index) =>
                Divider(height: 1, color: Colors.grey),
            itemCount: listData.data.length));
  }
}
